<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../_inc/funct.cerBil.php');
require_once('../../_inc/funct.tabDati.php');

if (reqVar('azione')=='conferma') {
	$msgPag .= conferma();
}

$aVar = array();
$aDefDati = array();
$msg = aVarLoadBil(reqvar('id'),$aVar,$aDefDati,'B');
$statoLavCorr =  $aVar['B_STATO_LAV'];
$nota = $aVar['B_NOTA_BIL'];
$idSetDati = $aVar['B_ID_SET_DATI'];
$fonteBil = $aVar['B_FONTE_BIL'];

$sCmdL = '';

$sCmdR = '';
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class='cmd1' href=\"javascript:submAz('conferma');\">conferma</a>";
$sCmdR .= cmdTorna('torna');

$aOpzTesta = array();
testaHtml('', $msgPag, 'ratg',$aOpzTesta);
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm('frm1','',array('enctype'=>true));
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('id');
echo getHtmlHidden('idp');
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" align="center" colspan='20'><b>Azienda</b></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Cod.fisc.</td>
      <td class="txt1" align="left" colspan='7'><?=$aVar['B_COD_FISC']?></td>
      <td class="lab1" align="left" colspan='3'>Cod CCIAA</td>
      <td class="txt1" align="left" colspan='7'><?=$aVar['B_COD_CCIAA']?></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Ragione sociale</td>
      <td class="txt1" align="left" colspan='17'>
      	<a class='txt1' href='anagVis.php?id=<?=$aVar['B_ID_AZIENDA']?>&xsn=<?=XSN?>'>
      	<?=$aVar['B_RAG_SOC']?></a></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Sede</td>
      <td class="txt1" align="left" colspan='17'><?=$aVar['B_SEDE']?></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Cod.sett.</td>
      <td class="txt1" align="left" colspan='3'><?=$aVar['B_COD_SETT']?></td>
      <td class="txt1" align="left" colspan='14'><?=$aVar['B_DESCR_SETT']?></td>
   <tr>
      <td class="lab1" align="left" colspan='3'>Descrizione</td>
      <td class="txt1" align="left" colspan='17'><?
      	reqDef('descr',$aVar['B_DESCR_BIL']);
      	echo getHtmlText('descr',80,80);
      	?></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Data riferimento</td>
      <td class="txt1" align="left" colspan='7'><?
      	reqDef('data_rif',YMD2DMY($aVar['B_DATA_RIF']));
      	echo getHtmlDate('data_rif');
      	?></td>
      <td class="lab1" align="left" colspan='3'>Data inizio</td>
      <td class="txt1" align="left" colspan='7'><?
      	reqDef('data_iniz',YMD2DMY($aVar['B_DATA_INIZ']));
      	echo getHtmlDate('data_iniz');
      	?></td>
   </tr>
	<tr>
		<td class='tit1'align='center' colspan='20'><b>BILANCIO</b></td>
	<tr>
<?
if (trim($nota)!='') {
?>
	<tr>
		<td class='txt1'align='left' colspan='20'>
			<?
			reqDef('nota',$nota);
			getHtmlTextArea('nota',100,3);
			?>
		</td>
	<tr>
<?
}

$aGeDati = getGerarcDefDati($aDefDati,1);
//dbg_($aGeDati);
//dbg_($aVar);
foreach($aGeDati as $codX => $aX) {
	echoDato($codX,$aX,$aVar,0);
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td> 
   </tr>
</table>
<?
echo "</form>\n";

codaHtml();
exit();


function echoDato($codX, $aX, &$aVar, $liv) {
	global $objSess, $objDB;
	$aCSez = array(''=>'-- non sezione --');
	$aCSez['spatt'] = 'Stato patrimoniale - Attivo';
	$aCSez['sppas'] = 'Stato patrimoniale - Passivo';
	$aCSez['cecon'] = 'Conto economico';
	$aCSez['altro'] = 'Altri dati';
	$aCSez['calco'] = 'Dati calcolati';
	
	if ($aX['attr']['cod_sez']!='') {
		echo "<tr><td class='txt1'align='center' colspan='12'><b>".$aCSez[$aX['attr']['cod_sez']]."</b></td>\n"
			."<td class='txt1' align='center' colspan='4'><i>".reqVar('data_rif')."</i></td>\n"
			."<td class='txt1' align='center' colspan='4'><i>Valori originari</i></td>\n"
			."<tr>\n";
	}
	$codV = 'B_'.$codX;
	$numX = '';
	$valX = '';
	switch ($aX['attr']['tipo_valore']) {
		case 'label':
			$bImp = false;
			break;
		case 'euro':
		case 'num':
			$bImp = true;
			$numX = (isset($aVar[$codV])) ? $aVar[$codV] : 0;
			$valX = number_format($numX,2,',','.');
			break;
	}
	if ($bImp) reqDef($codV,$valX);
	echo "<tr><td class='txt1' colspan='12'>".str_repeat("&nbsp;",$liv*8)
		."<a href='calcDato.php?cd=$codX&bil1=".reqVarInt('id')."&bil2=".reqVarInt('id_bil_prec')
			."&xsn=".XSN."' class='txt1'>$codX</a> - ".$aX['attr']['descr']."</td>"
		."<td class='txt1' align='right' colspan='4'>"
		.(($bImp)?getHtmlTextNum($codV,12):'&nbsp;')
		."</td>"
		."<td class='txt1' align='right' colspan='4'>"
		.$valX
		."</td>"
		."<tr>\n";
	foreach($aX['figli'] as $codY => $aY)  {
		echoDato($codY, $aY, $aVar, $liv+1);
	}
}

function conferma() {
	global $objSess, $objDB;
	$msg = '';
	if (reqVarStr2date('data_rif')===false) {
		$msg .= "data rif. errata<br>";
	}
	if (reqVarStr2date('data_iniz')===false) {
		$msg .= "data iniz. errata<br>";
	}
	if ($msg=='' 
	&& reqVarStr2YMD('data_rif') <= reqVarStr2YMD('data_iniz')) {
		$msg .= "data rif. precedente data iniz.<br>";
	}
	$aDati = array();
	$aVar = array();
	$aDefDati = array();
	$sSql = "SELECT str_def_dati FROM rgl_bilancio WHERE id_bil = ".reqVar('id');
	$objDB->query($sSql);
	$aRec = $objDB->fetch();
	if (!$aRec) {
		return 'no bil. '.reqVar('id');
	}
	$aDefDati = unsgzu($aRec['str_def_dati']);
	foreach($aDefDati as $cX => $aX) {
		dbg_($cX."=".reqVar('B_'.$cX));
		if (reqVarStr2num('B_'.$cX)===false) {
			$msg .= "Importo $cX errato<br>";
		} elseif (reqVarStr2num('B_'.$cX)!=0) {
			$aDati[$cX] = reqVarStr2num('B_'.$cX);
		}
	}
	if ($msg != '') return $msg;
	
	$sSql = "UPDATE rgl_bilancio SET descr = '".reqVarAS('descr')."'"
		.",data_rif = '".reqVarStr2YMD('data_rif')."'"
		.",data_iniz = '".reqVarStr2YMD('data_iniz')."'"
		.",str_dati = '".asgzcser($aDati)."'"
		." WHERE id_bil = ".reqVar('id');
	$objDB->exec($sSql);
	header("Location: ".urlTorna());
	exit();
	
	return "??????????";
}
	